package com.easyedu.dean.util.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.easyedu.dean.entity.excel.Student2Excel;
import com.easyedu.dean.service.StudentService;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lmr
 **/
public class SaveStuData extends AnalysisEventListener<Student2Excel> {

     private static final int BATCH_COUNT = 3000;
     List<Student2Excel> list = new ArrayList<>();

     private StudentService studentService;

     public SaveStuData(StudentService studentService) {
          this.studentService = studentService;
     }

     @Override
     public void invoke(Student2Excel data, AnalysisContext analysisContext) {
          list.add(data);
          if (list.size() >= BATCH_COUNT) {
               saveData();
               list.clear();
          }
     }

     @Override
     public void doAfterAllAnalysed(AnalysisContext analysisContext) {
          saveData();
     }

     public void saveData() {
          studentService.insertStudentsFromExcel(list);
     }
}